package com.neusmart.fs.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.neusmart.fs.F;
import com.neusmart.fs.R;
import com.neusmart.fs.constants.API;
import com.neusmart.fs.model.City;
import com.neusmart.fs.model.Constellation;
import com.neusmart.fs.model.Country;
import com.neusmart.fs.model.Educations;
import com.neusmart.fs.model.MParam;
import com.neusmart.fs.model.Provinces;
import com.neusmart.fs.model.Sex;
import com.neusmart.fs.result.ResultGetCity;
import com.neusmart.fs.result.ResultGetConstelltion;
import com.neusmart.fs.result.ResultGetCountry;
import com.neusmart.fs.result.ResultGetEducation;
import com.neusmart.fs.result.ResultGetProvince;
import com.neusmart.fs.util.TimeUtil;
import com.neusmart.fs.view.pickview.MyTimePickerView;
import com.neusmart.fs.view.pickview.ProfileAreaPickerView;
import com.neusmart.fs.view.pickview.ProfileCityPickerView;
import com.neusmart.fs.view.pickview.ProfileConstellationPickerView;
import com.neusmart.fs.view.pickview.ProfileEducationPickerView;
import com.neusmart.fs.view.pickview.ProfileSexPickerView;
import com.neusmart.fs.view.pickview.view.ProfileProvincePickerView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//修改个人资料的第一个页面
public class ChangeUserMassageActivity extends DataLoadActivity implements View.OnClickListener,ProfileAreaPickerView.OnCountryPickListener, ProfileProvincePickerView.OnProvincePickListener,ProfileCityPickerView.OnCityPickListener,
        ProfileConstellationPickerView.OnConstellationPickListener,ProfileEducationPickerView.OnEducationPickListener, ProfileSexPickerView.OnSexPickListener{
    private EditText userName,weixinId,weight,professional,school,heightEditText;
    //private  String mName,mWeixin,mBirth,mSex,mConstellation,mWeight,mCountry,mProvince,mCity,mProfessional,mSchool,mEducationBackground,mHeight;
    private TextView birthday,country,province,city,constellation,educationBackground,sex;
    private LinearLayout provinceLayout,cityLayout;
    private Bundle bundle=new Bundle();
    private MyTimePickerView mStartBirthday;
    private ProfileAreaPickerView mProfileAreaPickerView;
    private ProfileProvincePickerView mProfileProvincePickerView;
    private ProfileCityPickerView mProfileCityPickerView;
    private ProfileConstellationPickerView mProfileConstellationPickerView;
    private ProfileEducationPickerView mProfileEducationPickerView;
    private ProfileSexPickerView mProfileSexPickerView;
    private int provinceId;
    private List<Sex> mSexList;
    private int sexs,education_background,constellations;
    @Override
    protected int getLayoutResID() {
        return R.layout.activity_change_user_massage;
    }

    @Override
    protected void init() {
        initView();
        setContent();
        setListener();
        loadData(API.GET_COUNTRY,false);
        loadData(API.GET_PROVINCE,false);
        loadData(API.GET_CONSTELLATION,false);
        loadData(API.GET_EDUCATION,false);

    }

    private void intoBundle() {
        bundle.putString("userName",userName.getText().toString());
        bundle.putString("weixinId",weixinId.getText().toString());
        bundle.putString("birthday",birthday.getText().toString());
        bundle.putString("sex",sex.getText().toString());
        bundle.putString("constellation",constellation.getText().toString());
        bundle.putString("weight",weight.getText().toString());
        bundle.putString("country",country.getText().toString());
        bundle.putString("province",province.getText().toString());
        bundle.putString("city",city.getText().toString());
        bundle.putString("professional",professional.getText().toString());
        bundle.putString("school",school.getText().toString());
        bundle.putString("educationBackground",educationBackground.getText().toString());
        bundle.putString("height",heightEditText.getText().toString());
    }

    private void setContent() {
        userName.setText(F.mUserInfo.getName());
        weixinId.setText(F.mUserInfo.getWeixin_id());
        if (F.mUserInfo.getBirthday_year()!=0){
            birthday.setText(F.mUserInfo.getBirthday_year()+"");
        }
        if (F.mUserInfo.getSex()==1){
            sex.setText("男");
        }else if(F.mUserInfo.getSex()==2){
            sex.setText("女");
        }
        constellation.setText(getResources().getStringArray(R.array.constellation)[F.mUserInfo.getConstellation()]);
        if (F.mUserInfo.getHight()!=0){
            heightEditText.setText(F.mUserInfo.getHight()+"");
        }
       if (F.mUserInfo.getWeight()!=0){
           weight.setText(F.mUserInfo.getWeight()+"");
       }
        if (F.mUserInfo.getCountry()!=null && "中国".equals(F.mUserInfo.getCountry()))
        {country.setText(F.mUserInfo.getCountry());
            provinceLayout.setVisibility(View.VISIBLE);
            cityLayout.setVisibility(View.VISIBLE);
            if (F.mUserInfo.getProvince()!=null){province.setText(F.mUserInfo.getProvince());}
            if (F.mUserInfo.getCity()!=null){ city.setText(F.mUserInfo.getCity());}
        }else if (F.mUserInfo.getCountry()==null){//新用户
            country.setText("中国");
            provinceLayout.setVisibility(View.VISIBLE);
            cityLayout.setVisibility(View.VISIBLE);
        }else {
            country.setText(F.mUserInfo.getCountry());
            provinceLayout.setVisibility(View.GONE);
            cityLayout.setVisibility(View.GONE);
        }
        professional.setText(F.mUserInfo.getProfessional());
        school.setText(F.mUserInfo.getGraduate_school());
        if (F.mUserInfo.getEducation_background()==1){
            educationBackground.setText("硕士");
        }else if (F.mUserInfo.getEducation_background()==0){
            educationBackground.setText("本科");
        }else if (F.mUserInfo.getEducation_background()==2){
            educationBackground.setText("博士");
        }
    }
    private void initView() {
        mSexList=new ArrayList<>();
        Sex gril=new Sex();
        Sex man=new Sex();
        man.setSex("男");
        gril.setSex("女");
        mSexList.add(man);
        mSexList.add(gril);
        //起始年份
        mStartBirthday = new MyTimePickerView(this,MyTimePickerView.Type.YEAR);
        mStartBirthday.setRange(1950, 2000);
        mStartBirthday.setTime(new Date(50,1,0));
        mStartBirthday.setCyclic(false);
        //mStartYear = "1946";
        //tvSearchStartYear.setText(mStartYear);
        //
        mProfileAreaPickerView = new ProfileAreaPickerView(this,this);//国家选择
        mProfileProvincePickerView = new ProfileProvincePickerView(this,this);//省份选择
        mProfileCityPickerView = new ProfileCityPickerView(this,this);//城市选择
        mProfileConstellationPickerView=new ProfileConstellationPickerView(this,this);
        mProfileEducationPickerView=new ProfileEducationPickerView(this,this);
        mProfileSexPickerView=new ProfileSexPickerView(this,this);
        mProfileSexPickerView.setSex((ArrayList<Sex>) mSexList);
        //
        provinceLayout= (LinearLayout) findViewById(R.id.province_layout);
        cityLayout= (LinearLayout) findViewById(R.id.city_Layout);
        //
        userName= (EditText) findViewById(R.id.username_change);
        weixinId= (EditText) findViewById(R.id.weixin_change);
        birthday= (TextView) findViewById(R.id.brithday_change);
        sex= (TextView) findViewById(R.id.sex_change);
        constellation= (TextView) findViewById(R.id.constellation_change);
        weight= (EditText) findViewById(R.id.weight_change);
        country= (TextView) findViewById(R.id.country_change);
        province= (TextView) findViewById(R.id.province_change);
        city= (TextView) findViewById(R.id.city_change);
        professional= (EditText) findViewById(R.id.professional_change);
        school= (EditText) findViewById(R.id.school_change);
        educationBackground= (TextView) findViewById(R.id.education_change);
        heightEditText= (EditText) findViewById(R.id.height_change);

    }
    private void setListener() {
        int[] resIds = {R.id.my_data_btn_back,R.id.btn_next,R.id.brithday_change,R.id.country_change,R.id.province_change,R.id.city_change,
                R.id.constellation_change,R.id.education_change,R.id.sex_change};
        for (int id : resIds)
            findViewById(id).setOnClickListener(this);
        mStartBirthday.setOnTimeSelectListener(new MyTimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date) {
                birthday.setText(TimeUtil.getTimeByFormat(date, TimeUtil.DATE_FORMAT_Y));
            }
        });

    }


    @Override
    public void onClick(View v) {
        if (getCurrentFocus() != null)//隐藏软键盘
            hideSoftInput();
        switch (v.getId()) {
            case R.id.my_data_btn_back:
                onBackPressed();
                break;
            case R.id.brithday_change://选择出生日期
                mStartBirthday.show();
                break;
            case R.id.country_change://选择国家
                mProfileAreaPickerView.show();
                break;
            case R.id.province_change://选择省会
                mProfileProvincePickerView.show();
                break;
            case R.id.city_change://选择城市
                mProfileCityPickerView.show();
                break;
            case R.id.constellation_change:
                mProfileConstellationPickerView.show();
                break;
            case R.id.sex_change:
                mProfileSexPickerView.show();
                break;
            case R.id.education_change:
                mProfileEducationPickerView.show();
                break;
            case R.id.btn_next:
                bundle.putString("userName",userName.getText().toString());
                bundle.putString("weixinId",weixinId.getText().toString());
                bundle.putString("birthday",birthday.getText().toString());
                bundle.putString("sex",sex.getText().toString());
                bundle.putString("constellation",constellation.getText().toString());
                bundle.putString("weight",weight.getText().toString());
                bundle.putString("country",country.getText().toString());
                bundle.putString("province",province.getText().toString());
                bundle.putString("city",city.getText().toString());
                bundle.putString("professional",professional.getText().toString());
                bundle.putString("school",school.getText().toString());
                bundle.putString("educationBackground",educationBackground.getText().toString());
                bundle.putString("height",heightEditText.getText().toString());
                if (userName.getText().toString().length()<=0 || weixinId.getText().toString().length()<=0 || birthday.getText().toString().length()<=0 ||
                sex.getText().toString().length()<=0 || constellation.getText().toString().length()<=0 || weight.getText().toString().length()<=0 ||
                        country.getText().toString().length()<=0 || province.getText().toString().length()<=0 || city.getText().toString().length()<=0 ||
                        professional.getText().toString().length()<=0 || school.getText().toString().length()<=0 || educationBackground.getText().toString().length()<=0 ||
                heightEditText.getText().toString().length()<=0){
                    showToast("请完善信息");
                    return;
                }else {
                    if (F.mUserInfo.getUser_status()==0){//新用户
                        loadData(API.UPDATE_PROFILE, true);
                    }else {
                        intoBundle();
                        switchActivity(ChangeUserMassageNextActivity.class,bundle);
                    }

                }
                break;
            default:
                break;
        }
    }
    @Override
    protected void initParams(MParam param) {
        switch (param.getApi()) {
            case GET_COUNTRY:
                break;
            case GET_PROVINCE:
                break;
            case GET_CITY:
                param.addParam("provinceId", provinceId);
                break;
            case GET_CONSTELLATION:
                break;
            case GET_EDUCATION:
                break;
            case UPDATE_PROFILE:
                if ("男".equals(sex.getText().toString())){
                    sexs=1;
                }else if ("女".equals(sex.getText().toString())){
                    sexs=2;
                }
                if ("硕士".equals(educationBackground.getText().toString())){
                    education_background=1;
                }else if ("本科".equals(educationBackground.getText().toString())){
                    education_background=0;
                }else if ("博士".equals(educationBackground.getText().toString())){
                    education_background=2;
                }
                int length = getResources().getStringArray(R.array.constellation).length;
                for (int i=0;i<length;i++){
                    if (constellation.getText().toString().equals(getResources().getStringArray(R.array.constellation)[i])){
                        constellations=i;
                    }
                }
                param.addParam("hight", heightEditText.getText().toString());
                param.addParam("sex", sexs);
                param.addParam("province", province.getText().toString());
                param.addParam("graduate_school", school.getText().toString());
                param.addParam("weixin_id", weixinId.getText().toString());
                param.addParam("weight", weight.getText().toString());
                param.addParam("education_background", education_background);
                param.addParam("professional", professional.getText().toString());
                param.addParam("birthday_year", birthday.getText().toString());
                param.addParam("constellation", constellations);
                param.addParam("city", city.getText().toString());
                param.addParam("country", country.getText().toString());
                param.addParam("name", userName.getText().toString());//1
                param.addParam("needUpdateStatus",false);//1
                break;
            default:
                break;
        }
    }

    @Override
    protected void disposeResult(API api, String response) {
        if (response == null)
            return;
        switch (api) {
            case GET_COUNTRY:
                ResultGetCountry resultGetCountry = fromJson(response, ResultGetCountry.class);
                if (!resultGetCountry.isSuccess())
                    return;
                mProfileAreaPickerView.setCountry((ArrayList<Country>) resultGetCountry.getData().getCountrys());
                break;
            case GET_PROVINCE:
                ResultGetProvince resultGetProvince = fromJson(response, ResultGetProvince.class);
                if (!resultGetProvince.isSuccess())
                    return;
                mProfileProvincePickerView.setProvince((ArrayList<Provinces>) resultGetProvince.getData().getProvinces());
                if (F.mUserInfo.getProvince()==null || F.mUserInfo.getProvince().length()==0){//新用户
                    provinceId=110000;
                    loadData(API.GET_CITY,false);
                }else {
                    for (int i=0;i<resultGetProvince.getData().getProvinces().size();i++){
                        if (F.mUserInfo.getProvince().equals(resultGetProvince.getData().getProvinces().get(i).getProvince())){
                            provinceId=resultGetProvince.getData().getProvinces().get(i).getProvinceId();
                            loadData(API.GET_CITY,false);
                            return;
                        }
                    }
                }

                break;
            case GET_CITY:
                ResultGetCity resultGetCity = fromJson(response, ResultGetCity.class);
                if (!resultGetCity.isSuccess()){
                    return;
                }
                if (F.mUserInfo.getCity()==null){
                    city.setText(resultGetCity.getData().getCitys().get(0).getCity());
                }
                mProfileCityPickerView.setCity((ArrayList<City>) resultGetCity.getData().getCitys());
                break;
            case GET_CONSTELLATION:
                ResultGetConstelltion resultGetConstelltion = fromJson(response, ResultGetConstelltion.class);
                if (!resultGetConstelltion.isSuccess())
                    return;
                mProfileConstellationPickerView.setConstellation((ArrayList<Constellation>) resultGetConstelltion .getData().getConstellation());
                break;

            case GET_EDUCATION:
                ResultGetEducation resultGetEducation = fromJson(response, ResultGetEducation.class);
                if (!resultGetEducation.isSuccess())
                    return;
                mProfileEducationPickerView.setEducation((ArrayList<Educations>) resultGetEducation .getData().getEducation());
                break;
            case UPDATE_PROFILE:
                intoBundle();
                switchActivity(ChangeUserMassageNextActivity.class,bundle);
                break;
            default:
                break;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        //initSwipeBackFinish(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //onBackPressed();
    }

    @Override
    public void onCountryPick(Country countrys) {//选择国家如果是中国就显示省会和城市否则不显示
        country.setText(countrys.getCountry());
        if (countrys.getCountryId()==0){//代表中国
            provinceLayout.setVisibility(View.VISIBLE);
            cityLayout.setVisibility(View.VISIBLE);
        }else {
            provinceLayout.setVisibility(View.GONE);
            cityLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void onProvincePick(Provinces provinces) {
        province.setText(provinces.getProvince());
        provinceId=provinces.getProvinceId();
        //Log.i("kkk", "onProvincePick: "+provinceId);
        //根据省会从而的到它的城市列表
        loadData(API.GET_CITY,false);
    }

    @Override
    public void onCityPick(City citys) {
        city.setText(citys.getCity());
    }

    @Override
    public void onConstellationPick(Constellation constellations) {
        constellation.setText(constellations.getName());

    }

    @Override
    public void onEducationPick(Educations education) {
        educationBackground.setText(education.getName());

    }
    @Override
    public void onSexPick(Sex sexs) {
      sex.setText(sexs.getSex());
    }
}
